package org.Panel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.Component.PostBar;
import org.Component.PostMenuBar;
import org.Component.SearchPanel;
import org.JsonMsg.Post;
import org.Tools.StringOP;
import org.implement.Panel.PanelSize;
import org.implement.Panel.PanelSkip;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class MainPostPanel extends FaPanel{

    //置顶的主页菜单条
    public PostMenuBar postMenuBar;

    //内置的滚动面板
    public ScrollPanel scrollPanel;


    //搜索栏并入菜单条
//    public SearchPanel searchPanel;

    //单条帖子显示
//    public PostBar postBar;

    //放帖子的面板
    JPanel PostBars = new JPanel();

//    JScrollPane PostBarsScroll = new JScrollPane(PostBars);

    //母界面接口
    PanelSize panelSize;
    PanelSkip panelSkip;

    //帖子列表
    List<Post> Plist = new ArrayList<>();

    //记录要添加的帖子的长度
    int CHeight = 0;

    public MainPostPanel(PanelSize panelSize, PanelSkip panelSkip) {

        //界面id
        PanelIndex = 4;

        //实现接口
        this.panelSize = panelSize;
        this.panelSkip = panelSkip;

        //主页属性
        setBounds(0,0,panelSize.getWidth(),panelSize.getHeight());
        setLayout(null);

        //滚动界面
        scrollPanel = new ScrollPanel(getWidth(), getHeight()-100);
        scrollPanel.setBounds(0,100,getWidth(),getHeight()-100);
//        PostBarsScroll.setBounds(0,200,getWidth(),getHeight()-200);

        //帖子面板
//        PostBars = new JPanel();
        PostBars.setLayout(null);
        PostBars.setBounds(0,0,getWidth(),100);
        //改背景颜色
        PostBars.setBackground(Color.LIGHT_GRAY);
//        JLabel lb = new JLabel("Post Bars");
//        lb.setBounds(0,0,getWidth(),100);
//        PostBars.add(lb);
        scrollPanel.setPanel(PostBars);



//        for(int i = 1;i<=10;i++) {
//            //测试添加
//            PostBar postBar = new PostBar(panelSize,panelSkip,"hhhhhhhh","2012-10-12 13:23:56","svc");
//            addPost(postBar);
//        }

        //菜单条
        postMenuBar = new PostMenuBar(panelSize, panelSkip);
        postMenuBar.setBounds(0,0,getWidth(),100);

        //添加至界面
        add(scrollPanel);
        add(postMenuBar);


    }

    //添加单条帖子
    public void addPost(PostBar postBar) {
        //添加帖子
        PostBars.add(postBar);
        postBar.setLocation(50,CHeight+5);
        postBar.setSize(getWidth()-100,100);
        //更新聊天框的长度
        CHeight += postBar.getHeight()+5;

        PostBars.setSize(PostBars.getWidth(),CHeight);
    }

    //更新
    public void updatePanel() {
        //初始设置
        PostBars = new JPanel();
        CHeight = 0;
        PostBars.setLayout(null);
        PostBars.setBounds(0,0,getWidth(),0);

        StringOP op1 = new StringOP();
        //当前全部帖子
        String msg = MainPanel.AllPost;
        msg = op1.JsonStr(msg);
        System.out.println(msg);

        Gson gson = new Gson();
        // 由于要解析为List<Post>类型，需要使用TypeToken来获取准确的类型信息
        Type listType = new TypeToken<List<Post>>() {}.getType();
        List<Post> postList = gson.fromJson(msg, listType);

        //帖子更新
        for(Post post : postList) {
            PostBar postBar = new PostBar(panelSize,panelSkip,post);
            addPost(postBar);
        }


//        for(int i = 1;i<=10;i++) {
//            //测试添加
//            Post post = new Post();
//            post.setTitle("hhhhhhhh"+i);
//            post.setUpdatedAt("2012-10-12 13:23:56");
//            PostBar postBar = new PostBar(panelSize,panelSkip,post);
//            addPost(postBar);
//        }
        scrollPanel.setPanel(PostBars);
    }


    public void Run() {

        //运行逻辑
        scrollPanel.run();

        //输出相关信息
//        System.out.println("mainPostPanel");
//        System.out.println(getWidth());
//        System.out.println(getHeight());

    }

}
